package com.problem.stack;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yyh
 * @date 2024年11月11日 下午7:53
 * <a href="https://leetcode.cn/problems/simplify-path/?envType=study-plan-v2&envId=top-interview-150">...</a>
 */
public class SimplifyPath {

    public String simplifyPath(String path) {
        System.out.println(path);
        if (path == null) {
            return "/";
        }

        String[] split = path.split("/");

        List<String> l = new ArrayList<>();

        for (String s : split) {
            if ("".equals(s)) {
                continue;
            } else if ("..".equals(s)) {
                l.remove(l.size()-1);
            } else {
                l.add(s);
            }
        }
        StringBuilder res = new StringBuilder("/");

        for (String s : l) {
            res.append(s).append("/");
        }

        String p = res.toString();
        if (p.length() > 1) {
            p = p.substring(0, p.length() - 1);
        }
        return p;

    }

    public static void main(String[] args) {
        SimplifyPath path = new SimplifyPath();
        System.out.println(path.simplifyPath("/home//foo/"));
        System.out.println(path.simplifyPath("/home/"));
        System.out.println(path.simplifyPath("/home/user/Documents/../Pictures/"));

    }
}
